﻿<UserControl x:Class="Core.Views.ResultSearchView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:class="clr-namespace:Core.Class"
             xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:helpers="clr-namespace:Helpers;assembly=Helpers"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.Resources>

        <!--  Selector pour les templates des résultats  -->
        <class:ResultSearchTemplateSelector x:Key="ResultSearchTemplateSelector" />

        <!--  Template des résultats groupés par module  -->
        <DataTemplate x:Key="ItemResults">
            <Grid Margin="60,10,50,0" VerticalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="40" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <!--  Module  -->
                <StackPanel Grid.Row="0"
                            HorizontalAlignment="Left"
                            Orientation="Vertical">
                    <TextBlock Style="{StaticResource HeaderZoneResult}" Text="{Binding Module}" />
                </StackPanel>

                <!--  Résultats  -->
                <ListBox x:Name="ResultModule"
                         Grid.Row="1"
                         VerticalAlignment="Stretch"
                         Background="Transparent"
                         BorderThickness="0"
                         ItemTemplateSelector="{StaticResource ResultSearchTemplateSelector}"
                         ItemsSource="{Binding Path=Results}"
                         SelectedItem="{Binding Path=DataContext.SelectedResult,
                                                Mode=TwoWay,
                                                RelativeSource={RelativeSource Mode=FindAncestor,
                                                                               AncestorLevel=1,
                                                                               AncestorType=UserControl}}"
                         Visibility="{Binding Results.Count,
                                              Converter={StaticResource CountPositiveValueToVisibility}}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Margin="0,5,0,0"
                                         VerticalAlignment="Top"
                                         Rows="4" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>

                <TextBlock Grid.Row="1"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           FontSize="11pt"
                           Foreground="{StaticResource ForegroundThemeBrush}"
                           Text="{helpers:Localize Search_NoResult}"
                           Visibility="{Binding Path=Visibility,
                                                ElementName=ResultModule,
                                                Converter={StaticResource InvertVisibilityConverter}}" />
            </Grid>
        </DataTemplate>

    </UserControl.Resources>

    <controls:MetroContentControl>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <controls:ProgressRing x:Name="ProgressSearch"
                                   Grid.Row="0"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Foreground="{DynamicResource ColorThemeBrush}"
                                   IsActive="True"
                                   Visibility="{Binding IsSearching,
                                                        Converter={StaticResource BoolToVisibilityWithParamConverter}}" />
            <ScrollViewer Grid.Row="0"
                          ScrollViewer.HorizontalScrollBarVisibility="Auto"
                          ScrollViewer.VerticalScrollBarVisibility="Auto"
                          Visibility="{Binding Path=Visibility,
                                               ElementName=ProgressSearch,
                                               Converter={StaticResource InvertVisibilityConverter}}">
                <ListBox x:Name="ResultBox"
                         Margin="80,80,80,80"
                         VerticalContentAlignment="Stretch"
                         BorderThickness="0"
                         ItemTemplate="{StaticResource ItemResults}"
                         ItemsSource="{Binding Path=Results}"
                         ScrollViewer.CanContentScroll="False"
                         Style="{StaticResource ListBoxTransparentWithItemsTransparentHorizontal}"
                         Visibility="{Binding Path=Results.Count,
                                              Converter={StaticResource CountPositiveValueToVisibility}}" />
            </ScrollViewer>
        </Grid>
    </controls:MetroContentControl>
</UserControl>
